1193. Monthly Transactions I


Posted by ikl258794613 on 2024-02-23

Table: Transactions

Column Name Type
id int
country varchar
state enum
amount int
trans_date date

id is the primary key of this table.
The table has information about incoming transactions.
The state column is an enum of type ["approved", "declined"].

Write an SQL query to find for each month and country, the number of transactions and their total amount, the number of approved transactions and their total amount.

Return the result table in any order.

The query result format is in the following example.

Example 1:

Input:
Transactions table:

id country state amount trans_date
121 US approved 1000 2018-12-18
122 US declined 2000 2018-12-19
123 US approved 2000 2019-01-01
124 DE approved 2000 2019-01-07

Output:

month country trans_count approved_count trans_total_amount approved_total_amount
2018-12 US 2 1 3000 1000
2019-01 US 1 1 2000 2000
2019-01 DE 1 1 2000 2000
SELECT DATE_FORMAT(trans_date, '%Y-%m') AS month,
country,
COUNT(id) AS trans_count,
SUM(IF(state = 'approved',1,0) ) AS approved_count,
SUM(amount)AS trans_total_amount,
SUM(IF(state = 'approved',amount,0) ) AS approved_total_amount
FROM Transactions
GROUP BY month , country

trans_count 因為GROUP BY month , country 所以 2019-01 會分成兩筆。


#SQL







Related Posts

第二章:5 將 Filter 打包

第二章:5 將 Filter 打包

JAVA筆記_集合物件, 泛型, 序列化

JAVA筆記_集合物件, 泛型, 序列化

學習 Git (10) - Pull 與 Clone

學習 Git (10) - Pull 與 Clone


Comments